<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="站点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="site">-->
<!--              <j-single-select-tag-->
<!--                type="list_multi"-->
<!--                v-model="model.site"-->
<!--                dictCode="finance_amazon_site"-->
<!--                placeholder="请选择站点"-->
<!--              />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="国家" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="countryCode">
              <j-single-select-tag
                type="list_multi"
                v-model="model.countryCode"
                dictCode="lg_finance_amazon_country,country_name,country_code"
                placeholder="请选择国家"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="店铺" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shopName">
              <!-- <j-single-select-tag v-model="model.shopName" :options="shopOptions" placeholder="请选择店铺名称">
              </j-single-select-tag> -->
              <!-- <a-select
                v-model="model.shopName"
                style="width: 100%"
                placeholder="请选择店铺"
                @change="handleChange"
                :allowClear="allowClear"
              >
                <a-select-option v-for="(i, index) in shopOptions" :key="index" :value="i.value">
                  {{ i.text }}
                </a-select-option>
              </a-select> -->
               <j-search-select-tag v-model="model.shopName" :dict-options="shopOptions" placeholder="请选择店铺" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="isShowStoreMamager">
            <a-form-model-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storeMamager">
              <j-select-user-by-dep placeholder="请选择负责人,如果是自己可不填" v-model="model.storeMamager" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <!-- <a-form-model-item label="文件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="file">
              <a-upload :file-list="fileList" :remove="handleRemove" :before-upload="beforeUpload">
                <a-button> <a-icon type="upload" />上传文件</a-button>
              </a-upload>
            </a-form-model-item> -->
            <a-form-model-item label="文件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="file">
              <a-upload-dragger
                name="file"
                :file-list="fileList"
                :remove="handleRemove"
                :before-upload="beforeUpload"
              >
                <p class="ant-upload-drag-icon">
                  <a-icon type="inbox" />
                </p>
                <p class="ant-upload-text">单击或拖动文件到此区域进行上传</p>
              </a-upload-dragger>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { postAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import Vue from 'vue'
import { USER_INFO, ACCESS_TOKEN, TENANT_ID } from '@/store/mutation-types'
export default {
  name: 'FinanceAmazonOrderConfigForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {
        file: '',
        storeMamager: '',
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        countryCode: [{ required: true, message: '请选择国家' }],
        shopName: [{ required: true, message: '请选择店铺名称' }],
        site: [{ required: true, message: '请选择站点' }],
        file: [{ required: true, message: '请导入文件' }],
      },
      url: {
        importExcelUrl: 'amazon/financeAmazonOrder/importCsv',
        add: '/amazon/financeAmazonOrder/importCsv',
      },
      isShowStoreMamager: false, //是否隐藏负责人
      shopOptions: [], //店铺名称数据
      fileList: [],
      allowClear: true, //是否支持清除
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
    tokenHeader() {
      let head = { 'X-Access-Token': Vue.ls.get(ACCESS_TOKEN) }
      let tenantid = Vue.ls.get(TENANT_ID)
      if (tenantid) {
        head['tenant-id'] = tenantid
      }
      return head
    },
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    const userIdentity = Vue.ls.get(USER_INFO).userIdentity
    this.isShowStoreMamager = userIdentity == 2 ? true : false
    this.getListShop()
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    handleChange(value) {
      console.log(value)
      this.model.shopName = value
    },

    //删除文件
    handleRemove(file) {
      this.model.file = ''
      const index = this.fileList.indexOf(file)
      const newFileList = this.fileList.slice()
      newFileList.splice(index, 1)
      this.fileList = newFileList
    },
    //上传文件之前
    beforeUpload(file) {
      this.model.file = file
      this.fileList = [...this.fileList, file]
      this.fileList = this.fileList.slice(-1)
      return false
    },
    //获取店铺名称
    getListShop() {
      getAction('/amazon/financeAmazonOrder/queryShopList')
        .then((res) => {
          if (res.success) {
            this.shopOptions = res.result
          } else {
            this.$message.error(res.message)
          }
        })
        .catch((err) => {
          this.$message.error(err.message)
        })
        .finally(() => {})
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = new FormData()
          formData.append('countryCode', this.model.countryCode)
          formData.append('shopName', this.model.shopName)
          formData.append('site', this.model.site)
          formData.append('storeMamager', this.model.storeMamager)
          formData.append('file', this.model.file)
          postAction(httpurl, formData)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .catch((err) => {
              this.$message.error(err.message)
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>